package bean;

import com.zaxxer.hikari.HikariConfig;
import com.zaxxer.hikari.HikariDataSource;

import javax.sql.DataSource;
import java.io.UnsupportedEncodingException;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

public class AnswerDAO implements Utf8_{

    DataSource ds;

    public AnswerDAO() {
        HikariConfig config = new HikariConfig();
        config.setJdbcUrl("jdbc:mysql://localhost:3306/xmart?useUnicode=true&characterEncoding=UTF-8&useJDBCCompliantTimezoneShift=true&useLegacyDatetimeCode=false&serverTimezone=UTC");
        config.setDriverClassName("com.mysql.jdbc.Driver");
        config.setUsername("root");
        config.setPassword("123456");
        config.addDataSourceProperty("connectionTimeout", "1000"); // 连接超时：1秒
        config.addDataSourceProperty("idleTimeout", "60000"); // 空闲超时：60秒
        config.addDataSourceProperty("maximumPoolSize", "800"); // 最大连接数：10
        ds = new HikariDataSource(config);
    }


    public int add(Answer answer) {
        String sql = "insert into answer values (null,?,?,?,?,?)";
        try (Connection conn = ds.getConnection(); PreparedStatement ps = conn.prepareStatement(sql)) {
            ps.setString(1, transform(answer.getContent()));
            ps.setInt(2, 0);
            ps.setInt(3, 0);
            ps.setInt(4, answer.getresponder_id());
            ps.setInt(5, (int) answer.getTimeStamp());
            ps.execute();
            ResultSet rs =  ps.executeQuery("SELECT LAST_INSERT_ID()");
            if (rs.next()) {
                return rs.getInt(1);
            }
        } catch (SQLException | UnsupportedEncodingException e) {
            e.printStackTrace();
        }
        return -1;
    }

    public void delete(int id) {
        String sql = "delete from answer where id = " + id;
        try (Connection conn = ds.getConnection(); PreparedStatement ps = conn.prepareStatement(sql);) {
            ps.execute();
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }


    public Answer get(int id) {
        Answer answer = null;
        String sql = "select * from answer where id = " + id;
        try (Connection conn = ds.getConnection(); PreparedStatement ps = conn.prepareStatement(sql);) {
            ResultSet rs = ps.executeQuery();
            if (rs.next()) {
                answer = new Answer();
                answer.setAnswerID(rs.getInt("id"));
                answer.setContent(rs.getString("content"));
                answer.setLike(rs.getInt("like"));
                answer.setUnLike(rs.getInt("unlike"));
                answer.setresponder_id(rs.getInt("sponsor_id"));
                answer.setTimeStamp(rs.getInt("timestamp"));
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return answer;
    }

    public void updateAnswer(Answer answer) {

        String sql = "update answer set  content = ?, like = ?,unlike = ? where id = ?";
        try (Connection conn = ds.getConnection(); PreparedStatement ps = conn.prepareStatement(sql);) {
            ps.setString(1, transform(answer.getContent()));
            ps.setInt(2, answer.getLike());
            ps.setInt(3, answer.getUnLike());
            ps.setInt(3, answer.getAnswerID());
            ps.execute();
        } catch (SQLException | UnsupportedEncodingException e) {
            e.printStackTrace();
        }
    }

}
